<template><div><h1 id="生命周期" tabindex="-1"><a class="header-anchor" href="#生命周期" aria-hidden="true">#</a> 生命周期</h1>
<p><img src="@source/blog/Flutter/assets/944365-00711efb57df0915.png" alt="Widget生命周期"></p>
<h2 id="_1-initstate" tabindex="-1"><a class="header-anchor" href="#_1-initstate" aria-hidden="true">#</a> 1. <code v-pre>initState()</code></h2>
<p><strong>调用时机：</strong></p>
<p>当<code v-pre>Widget</code>对象第一次插入到<code v-pre>Widget</code>树时</p>
<p><strong>具体描述：</strong></p>
<p>对于每一个<code v-pre>State</code>对象，<code v-pre>Flutter framework</code>只会调用一次该回调</p>
<p><strong>应用场景：</strong></p>
<p>一次性操作，如状态初始化、订阅事件通知等</p>
<h2 id="_2-didchangedependencies" tabindex="-1"><a class="header-anchor" href="#_2-didchangedependencies" aria-hidden="true">#</a> 2. <code v-pre>didChangeDependencies()</code></h2>
<p><strong>调用时机：</strong></p>
<ul>
<li><code v-pre>inState()</code> 调用后</li>
<li>当<code v-pre>Widge</code>t对象的依赖发生变化时</li>
</ul>
<p><strong>具体描述：</strong></p>
<p>如：<code v-pre>Widget_a</code>里包含<code v-pre>Widget_b</code>，若之后<code v-pre>Widget_a</code>发生变化，那么<code v-pre>Widget_b</code>的<code v-pre>didChangeDependencies()</code>回调都会被调用</p>
<p><strong>应用场景：</strong></p>
<p>依赖性的链式反应操作（典型场景：当系统主题改变时，<code v-pre>Flutter</code>会通知<code v-pre>widget</code>调用此回调）</p>
<h2 id="_3-build" tabindex="-1"><a class="header-anchor" href="#_3-build" aria-hidden="true">#</a> 3. <code v-pre>build()</code></h2>
<p><strong>调用时机：</strong></p>
<ul>
<li>调用<code v-pre>initState()</code>后</li>
<li>调用<code v-pre>didUpdateWidget()</code>后</li>
<li>调用<code v-pre>setState()</code>后</li>
<li>调用<code v-pre>didChangeDependencies()</code>后</li>
<li>在<code v-pre>State</code>对象从树中一个位置移除后（调用<code v-pre>deactivate()</code>）有重新插入到树的其他位置</li>
</ul>
<p><strong>具体描述：</strong></p>
<p>最常用 <code v-pre>&amp;</code> 关键的生命周期方法</p>
<p><strong>应用场景：</strong></p>
<p>构建<code v-pre>Widget</code>子树（返回一个<code v-pre>Widget</code>）</p>
<h2 id="_4-deactivate" tabindex="-1"><a class="header-anchor" href="#_4-deactivate" aria-hidden="true">#</a> 4. <code v-pre>deactivate()</code></h2>
<p><strong>调用时机：</strong></p>
<p>当<code v-pre>Widget</code>对象从树中暂时被移除时</p>
<p><strong>具体描述：</strong></p>
<ul>
<li>很多时候，<code v-pre>Flutter</code>会将<code v-pre>State</code>对象重新插到树中</li>
<li>如：含此<code v-pre>State</code>对象的子树在树的一个位置移动到另一个位置时</li>
<li>重写时必须调用<code v-pre>super.deactivate()</code></li>
</ul>
<p><strong>应用场景：</strong></p>
<p>移动对象时进行更新操作</p>
<h2 id="_5-dispose" tabindex="-1"><a class="header-anchor" href="#_5-dispose" aria-hidden="true">#</a> 5. <code v-pre>dispose()</code></h2>
<p><strong>调用时机：</strong></p>
<p>当<code v-pre>Widget</code>对象从树中暂时被移除时</p>
<p><strong>具体描述：</strong></p>
<ul>
<li>直接将移除<code v-pre>Widget</code>对象 <code v-pre>&amp;</code> 不显示</li>
<li>重写时必须调用<code v-pre>super.deactivate()</code></li>
</ul>
<p><strong>应用场景：</strong></p>
<p>释放资源</p>
<h2 id="_6-didupdatewidget" tabindex="-1"><a class="header-anchor" href="#_6-didupdatewidget" aria-hidden="true">#</a> 6. <code v-pre>didUpdateWidget()</code></h2>
<p><strong>调用时机：</strong></p>
<p><code v-pre>Widget</code>配置发生变化时（<code v-pre>Widget.canUpdate</code>返回<code v-pre>true</code>）</p>
<p><strong>具体描述：</strong></p>
<ul>
<li><code v-pre>widget</code>重新构建时，<code v-pre>Flutter framework</code>会调用<code v-pre>Widget.canUpdate</code>来检测<code v-pre>Widget</code>树中同一位置的新旧节点，然后决定是否需要更新</li>
<li>此时<code v-pre>Widget.canUpdate</code>返回<code v-pre>true</code>则调用此回调</li>
</ul>
<p><strong>应用场景：</strong></p>
<p>更新<code v-pre>Widget</code></p>
<h2 id="_7-setstate" tabindex="-1"><a class="header-anchor" href="#_7-setstate" aria-hidden="true">#</a> 7.<code v-pre>setState()</code></h2>
<p><strong>调用时机：</strong></p>
<p>手动调用以此更新视图</p>
<p><strong>应用场景：</strong></p>
<p>页面有内容需要更新</p>
</div></template>


